//
//  ViewController.m
//  UI15_homework_网易新闻导航条(自适应文字长度)
//
//  Created by Hanlingyu on 16/8/3.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "LtqNavgationTitleFlowLayout.h"
#import "TitleCollectionViewCell.h"
#import "QCTitleViewLayout.h"
#define kCollectionViewReuse @"reuse"
#define kArcColor [UIColor colorWithRed:arc4random() % 255 / 256.0 green:arc4random() % 255 / 256.0 blue:arc4random() % 255 / 256.0 alpha:1];
@interface ViewController ()

<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
//    QCTitleViewLayoutDelegate,
    LtqWaterFlowLayoutDelegate
>

@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation ViewController

- (void)handleArray {
    
    _titleArray = @[@"头条", @"精选", @"NBA", @"娱乐", @"哒哒趣闻", @"云课堂", @"影视歌", @"中国足球", @"态度公开课", @"移动互联网", @"轻松一刻", @"体育"];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    // 将navgationBar的
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // 给这个界面上的scollView会默认加上边距, 将这个属性关掉
    // scrollView自动边距
    // 遇到状态栏, 导航栏, 自动为页面上的某一个scrollView添加边距
    // 一般在开发中, 关闭这个属性
    // 一般讲这连个方法写在基类里
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self handleArray];
    
    LtqNavgationTitleFlowLayout *layout = [[LtqNavgationTitleFlowLayout alloc] init];
    layout.delegate = self;
    
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collection];
    collection.dataSource = self;
    collection.delegate = self;
    [collection registerNib:[UINib nibWithNibName:@"TitleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewReuse];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
//    cell.titleLabel.font = [UIFont systemFontOfSize:12];
    cell.titleLabel.text = _titleArray[indexPath.row];
//    NSLog(@"%@", _titleArray[indexPath.row]);
    cell.titleLabel.backgroundColor = kArcColor;
    NSLog(@"%@", NSStringFromCGSize(cell.bounds.size));
    return cell;
    
    
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath height:(CGFloat)height{
//    NSString *string = _titleArray[indexPath.row];
//    
////    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
////    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
//    CGRect newRect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//    
//    
//    NSLog(@"%@---------%lf", string, newRect.size.width);
//    return newRect.size.width;
//}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath height:(CGFloat)height {
    NSString *string = _titleArray[indexPath.row];
    CGRect newRect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    CGFloat width = ceil(newRect.size.width);
    return width;
    
}

//- (CGSize)myCollectionView:(UICollectionView *)collectionView qc_layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSString *string = _titleArray[indexPath.row];
//    CGRect newRect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, collectionView.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//    return newRect.size;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
